package com.leyou.shop.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leyou.common.pojo.R;
import com.leyou.mall.data.entity.Brand;
import com.leyou.mall.data.entity.Result;
import com.leyou.mall.data.vo.BrandVo;
import com.leyou.shop.entity.OrderShop;
import com.leyou.shop.entity.UpdateLog;
import com.leyou.shop.entity.vo.OrderShopVo;
import com.leyou.shop.service.IOrderShopService;
import com.leyou.shop.service.IUpdateLogService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cyb
 * @since 2022-07-14
 */
@RestController
@RequestMapping("/order-shop")
public class OrderShopController {
    @Resource
    private IUpdateLogService updateLogService;

    @Resource
    private IOrderShopService orderShopService;


    @PostMapping("updateStatu")
    public Result updateStatu(UpdateLog updateLog) {
        System.err.println(updateLog.toString());
        orderShopService.updateStatu(updateLog);

        return Result.success();
    }

    /**
     * @description: 新增和修改
     * @author 车矣捕
     */
    @PostMapping
    public Result save(@RequestBody OrderShop orderShop) {
        orderShopService.saveOrUpdate(orderShop);
        return Result.success();
        }



    /**
     * @description: 删除
     * @author 车矣捕
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        orderShopService.removeById(id);
        return Result.success();
        }

    /**
     * @description: 批量删除
     * @author 车矣捕
     */
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        orderShopService.removeByIds(ids);
        return Result.success();
        }

    /**
     * @description: 查询所有
     * @author 车矣捕
     */
    @GetMapping
    public Result findAll() {

        return Result.success(orderShopService.list());
    }

    /**
     * @description: 根据id查询
     * @author 车矣捕
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(orderShopService.getById(id));
    }


    /**
     * @description:  分页查询
     * @author 车矣捕
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                            @RequestParam Integer pageSize) {
        QueryWrapper<OrderShop> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Result.success(orderShopService.page(new Page<>(pageNum, pageSize), queryWrapper));
        }


    @PostMapping("/orderList")
    public Result orderList(OrderShopVo vo){
        QueryWrapper<OrderShop> queryWrapper = new QueryWrapper<>();
        long pageNum=vo.getPageNum();
        long pageSize= vo.getPageSize();
        if (!"".equals(vo.getUserId()) && vo.getUserId()!=null){
            queryWrapper.like("user_id",vo.getUserId());
        }
        if (!"".equals(vo.getOrderNum())&& vo.getOrderNum()!=null){
            queryWrapper.like("order_num",vo.getOrderNum());
        }

        return Result.success(orderShopService.page(new Page<>(pageNum, pageSize),queryWrapper));
    }

    @GetMapping("/selectCount")
    public Result selectCount() {
        List<OrderShop> orderShops = orderShopService.selectCount();
        HashMap<String, Integer> map = new HashMap<>();

        for (int i = 1; i < 6; i++) {
            map.put("s"+i,0);
        }

        for (int i = 0; i < orderShops.size(); i++) {
            map.put("s"+orderShops.get(i).getStatu(),orderShops.get(i).getTotalByType());
        }
        return Result.success(map);
        }

}

